import DatabaseUtils from "../utils/db-utils";

class BattleHistoryDao {

    /**
     * 查询要复盘的对局详情
     * @param userId
     * @param battleId
     * @param pageSize
     */
    static getBattleReviewDetail = async (userId: string, battleId: string, pageSize: number) => {
        const countSql = `select count(*) as total from t_battle_map_history where battle_id = ? and user_id = ? `;
        const countRows: any = await DatabaseUtils.execSql(countSql, [battleId, userId]);
        const total = countRows.length === 0 ? 0 : countRows[0].total;
        if (total === 0) {
            return {total: 0, list: []};
        } else {
            const sql = `
              select  
                 game_map as gameMap,
                 step_count as stepCount,
                 is_red_move as isRedMove,
                 last_chess as lastChess,
                 last_chess_box as lastChessBox,
                 think_time as thinkTime
              from t_battle_map_history 
             where battle_id = ? and user_id = ? 
             order by step_count asc
             limit ?`;
            const params = [battleId, userId, pageSize];
            const dataRows: any = await DatabaseUtils.execSql(sql, params);
            return {
                total: total,
                list: dataRows
            }
        }
    };

    /**
     * 删除最后两步历史记录
     * @param battleId
     * @returns {Promise<boolean>}
     */
    static deleteLastMapHistory = async (battleId: string) => {
        const sql = `delete from t_battle_map_history where battle_id = ? order by create_time desc limit 2`;
        const params = [battleId];
        await DatabaseUtils.execSql(sql, params);
        return true;
    };

    /**
     * 查询该用户所在房间的历史移动步骤
     * @param battleId
     * @param userId
     * @returns {Promise<unknown>}
     */
    static getAllHistoryMoveStep = async (battleId: string, userId: string) => {
        const sql = `select game_map as gameMap, last_chess_box as lastChessBox, last_chess as lastChess from t_battle_map_history
                where battle_id = ? and user_id = ? order by create_time asc`;
        const params = [battleId, userId];
        return await DatabaseUtils.execSql(sql, params);
    };

    /**
     * 创建对战历史步骤数据
     * @param battleId
     * @param userId
     * @param gameMap
     * @param isRedMove
     * @param stepCount
     * @param thinkTime
     * @param lastChess
     * @param chessBox
     * @param allTime
     * @param stepTime
     */
    static createBattleMapHistory = async (battleId: string, userId: string, gameMap: string,
                                           isRedMove: boolean, stepCount: number, thinkTime: number | null,
                                           lastChess: string | null, chessBox: string, allTime: number, stepTime: number) => {
        const sql = `insert into t_battle_map_history(battle_id, user_id, game_map, is_red_move, step_count,
              think_time, last_chess, last_chess_box, all_time, step_time) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
        const params = [battleId, userId, gameMap, isRedMove, stepCount, thinkTime, lastChess, chessBox, allTime, stepTime];
        await DatabaseUtils.execSql(sql, params);
        return true;
    };

    /**
     * 查询最后一条历史对战数据
     * @param battleId
     * @param userId
     * @returns {Promise<void>}
     */
    static getLastBattleMapHistory = async (battleId: string, userId: string) => {
        // 此条SQL不能根据创建时间倒序，如果创建时间不够精确，则会造成取值错误
        const sql = `
              select b.battle_id       as battleId,
                   b.user_id           as userId,
                   b.game_map          as gameMap,
                   b.is_red_move       as isRedMove,
                   b.step_count        as stepCount,
                   b.last_chess        as lastChess,
                   b.last_chess_box    as lastChessBox,
                   b.all_time          as allTime,
                   b.step_time         as stepTime,
                   b.create_time       as createTime
            from t_battle_map_history b
            where battle_id = ?
              and user_id = ?
            order by step_count desc
            limit 1
          `;
        const params = [battleId, userId];
        const rows: any = await DatabaseUtils.execSql(sql, params);
        return rows.length === 0 ? null : rows[0];
    };

    /**
     * 查询对战历史步骤表
     * @param where
     * @returns {Promise<unknown>}
     */
    static queryBattleMapHistoryTable = async (where: any[]) => {
        const tableName = 't_battle_map_history';
        const columns = ['id', 'battle_id', 'user_id', 'game_map', 'is_red_move', 'step_count', 'think_time',
            'last_chess_box', 'game_map', 'last_chess', 'all_time', 'step_time', 'create_time', 'update_time'];
        const sqlObj = DatabaseUtils.getDynamicSelectObj(tableName, columns, where);
        return await DatabaseUtils.execSql(sqlObj.sql, sqlObj.params);
    };

    /**
     * 动态更新表
     * @param columns
     * @param where
     */
    static updateTable = async (columns: any[], where: any[]) => {
        const tableName = 't_battle_map_history';
        const sqlObj = DatabaseUtils.getDynamicUpdateObj(tableName, columns, where);
        return await DatabaseUtils.execSql(sqlObj.sql, sqlObj.params);
    }

}

export default BattleHistoryDao;